home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / amigem.lha / amigem / exec / libraries.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-28  |  4.5 KB  |  213 lines

  1. #include <exec/execbase.h>
  2. #include <exec/memory.h>
  3. #include <exec/semaphores.h>
  4. #include <exec/devices.h>
  5. #include <exec/io.h>
  6. #include <dos/dos.h>
  7. #include <amigem/utils.h>
  8. #include <clib/_exec.h>
  9.  
  10. #include <amigem/fd_lib.h>
  11. #define LIBBASE struct ExecBase *SysBase
  12.  
  13. FC1(1,struct Library *,Lib_Open,A6,ULONG version,D0)
  14. ;
  15. FC0(2,BPTR,Lib_Close,A6)
  16. ;
  17.  
  18. FD1(66,void,AddLibrary,struct Library *lib,A1)
  19. {
  20.   lib->lib_Node.ln_Type=NT_LIBRARY;  
  21.   lib->lib_Flags|=LIBF_CHANGED;
  22.   SumLibrary(lib); /* build checksum for library vectors */
  23.  
  24.   Forbid();
  25.     Enqueue(&SysBase->LibList,&lib->lib_Node);
  26.   Permit();
  27. }
  28.  
  29. FD1(67,void,RemLibrary,struct Library *lib,A1)
  30. {
  31.   Forbid();
  32.     Remove(&lib->lib_Node);
  33.   Permit();
  34. }
  35.  
  36. FD3(15,ULONG,MakeFunctions,APTR bp,A0,APTR array,A1,APTR base,A2)
  37. {
  38.   UBYTE *tp=(UBYTE *)base;
  39.  
  40.   if(base!=NULL)
  41.   {
  42.     WORD *fp=(WORD *)array;
  43.     while(*fp!=-1)
  44.     {
  45.       tp-=LIB_VECTSIZE;
  46.       MINSETFUNCTION(tp,(char *)base+*fp);
  47.       fp++;
  48.     }
  49.   }
  50.   else
  51.   {
  52.     void **fp=(void **)array;
  53.     while(*fp!=(void *)-1)
  54.     {
  55.       tp-=LIB_VECTSIZE;
  56.       MINSETFUNCTION(tp,*fp);
  57.       fp++;
  58.     }
  59.   }
  60.   CacheClearE(tp,(UBYTE *)base-tp,CACRF_ClearI|CACRF_ClearD);
  61.   return (UBYTE *)base-tp;
  62. }
  63.  
  64. void InitStructNoClear(APTR is,APTR mem,ULONG size)
  65. {
  66.   UBYTE *st,*de;
  67.   st=(UBYTE *)is;
  68.   de=(UBYTE *)mem;
  69.  
  70.   while(*st!=0)
  71.   {
  72.     LONG cnt1,max2,cnt2;
  73.     UBYTE t;
  74.  
  75.     cnt1=(*st&15)+1;
  76.     t=*st>>6&3;
  77.     max2=4>>(*st++>>4&3);
  78.     if(max2!=1&&t!=2)
  79.       st++;
  80.  
  81.     switch(t)
  82.     {
  83.       case 0:
  84.         for(;cnt1;cnt1--)
  85.           for(cnt2=max2;cnt2;cnt2--)
  86.             *de++=*st++;
  87.         break;
  88.       case 1:
  89.         for(;cnt1;cnt1--)
  90.         {
  91.           for(cnt2=max2;cnt2;cnt2--)
  92.             *de++=*st++;
  93.           st-=max2;
  94.         }
  95.         st+=max2;
  96.         break;
  97.       case 2:
  98.         de=(UBYTE *)mem+*st++;
  99.         for(;cnt1;cnt1--)
  100.           for(cnt2=max2;cnt2;cnt2--)
  101.             *de++=*st++;
  102.         break;          
  103.       case 3:
  104.       {/* A 24 bit offset on a 32 bit machine is very nasty - this solution is even more */
  105.         ULONG test_endian=0x18100800;
  106.         UBYTE *et=(UBYTE *)&test_endian;
  107.         if(*et==0x18)
  108.           et++;
  109.         de=(UBYTE *)mem+((st[0]<<et[0])+(st[1]<<et[1])+(st[2]<<et[2]));
  110.         for(;cnt1;cnt1--)
  111.           for(cnt2=max2;cnt2;cnt2--)
  112.             *de++=*st++;
  113.         break;          
  114.       } 
  115.     } 
  116.   }
  117. }
  118.  
  119. FD3(13,void,InitStruct,APTR is,A1,APTR mem,A2,ULONG size,D0)
  120. {
  121.   { /* Clear Memory area */
  122.     UBYTE *b=mem;
  123.     ULONG s2=size;
  124.     while(s2--)
  125.       *b++=0;
  126.   }
  127.   InitStructNoClear(is,mem,size);
  128. }
  129.  
  130. FD5(14,struct Library *,MakeLibrary,APTR jmptabl,A0,APTR is,A1,ULONG (*initpc)(),A2,ULONG size,D0,ULONG seglist,D1)
  131. {
  132.   struct Library *ret;
  133.   ULONG jtsize=0,possize,negsize;
  134.  
  135.   {/* Count jumpvectors */
  136.     if(*(WORD *)jmptabl==-1)
  137.     {
  138.       WORD *fp=(WORD *)jmptabl+1;
  139.       while(*fp++!=-1)
  140.         jtsize+=LIB_VECTSIZE;
  141.     }
  142.     else
  143.     {
  144.       void **fp=(void **)jmptabl;
  145.       while((long)*fp++!=-1)
  146.         jtsize+=LIB_VECTSIZE;
  147.     }
  148.   }
  149.  
  150.   negsize=(jtsize+(sizeof(ULONG)-1))&~(sizeof(ULONG)-1);
  151.   possize=(size+(sizeof(ULONG)-1))&~(sizeof(ULONG)-1);
  152.  
  153.   if((ret=(struct Library *)AllocMem(possize+negsize,MEMF_PUBLIC|MEMF_CLEAR))!=NULL)
  154.   {
  155.     ret=(struct Library *)((char *)ret+negsize);
  156.  
  157.     if(*(WORD *)jmptabl==-1)
  158.       MakeFunctions(ret,(WORD *)jmptabl+1,(WORD *)jmptabl+1);
  159.     else
  160.       MakeFunctions(ret,jmptabl,NULL);
  161.  
  162.     ret->lib_NegSize=negsize;
  163.     ret->lib_PosSize=possize;
  164.     if(is!=NULL)
  165.       InitStructNoClear(is,ret,size);
  166.     if(initpc!=NULL)
  167.       ret=(*(struct Library *(*)(struct Library *,BPTR,struct ExecBase *))
  168.             initpc)(ret,seglist,SysBase);
  169.   }
  170.   return ret;
  171. }
  172.  
  173. FD1(68,struct Library *,OldOpenLibrary,APTR libName,A1)
  174. {
  175.   return OpenLibrary(libName,0);
  176. }
  177.  
  178. FD1(69,void,CloseLibrary,struct Library *library,A1)
  179. {
  180.   Forbid();
  181.     if(library)
  182.       Lib_Close(library);
  183.   Permit();
  184. }
  185.  
  186. FD3(70,APTR,SetFunction,struct Library *library,A1,LONG funcOffset,A0,APTR funcEntry,D0)
  187. {
  188.   APTR ret;
  189.   library->lib_Flags|=LIBF_CHANGED;
  190.   ret=MINGETFUNCTION((char *)library+funcOffset);
  191.   MINSETFUNCTION((char *)library+funcOffset,funcEntry);
  192.   CacheClearE((char *)library+funcOffset,LIB_VECTSIZE,CACRF_ClearI|CACRF_ClearD);
  193.   SumLibrary(library);
  194.   return ret;
  195. }
  196.  
  197. FD1(71,void,SumLibrary,struct Library *library,A1)
  198. {
  199. }
  200.  
  201. FD2(92,struct Library *,OpenLibrary,STRPTR libName,A1,ULONG version,D0)
  202. {
  203.   struct Library *ret;
  204.   Forbid();
  205.     ret=(struct Library *)FindName(&SysBase->LibList,libName);
  206.     if(ret->lib_Version<version)
  207.       ret=NULL;
  208.     if(ret)
  209.       ret=Lib_Open(ret,version);
  210.   Permit();
  211.   return ret;
  212. }
  213.